<template>
  <div class="container">
    <!-- <div class="top-nav">
      <div @click="tab = 0" :class="{ 'nav-cell': true, active: tab === 0 }">
        <span>普通商品</span>
      </div>
      <div @click="tab = 1" :class="{ 'nav-cell': true, active: tab === 1 }">
        <span>会员商品</span>
      </div>
    </div> -->
    <div class="cart-list with-grey-bottom-border">
      <div v-if="cartList.length">
        <yd-checklist
          ref="checklist"
          color="#0078FF"
          v-model="checkItemList"
          :callback="change"
        >
          <div v-for="item in cartList" :key="item.id" class="cart-cell">
            <yd-checklist-item
              @click.stop
              style="position:absolute; left:0.12rem; top:1.24rem;"
              :val="item.id"
            >
            </yd-checklist-item>
            <div
              @click="$router.push('/item-detail/' + item.commodity_id)"
              class="left"
            >
              <img :src="item.spec_img" alt="" />
            </div>
            <div
              @click="$router.push('/item-detail/' + item.commodity_id)"
              class="middle"
            >
              <div class="item-name">
                {{ item.name }}
              </div>
              <div class="item-size">规格：{{ item.spec_name }}</div>
              <div class="price">
                ¥ {{ level > 0 ? item.discount_price : item.spe_price }}
              </div>
            </div>

            <yd-spinner
              class="cart-number"
              style="margin-top:0.4rem"
              width="3.2rem"
              height="1rem"
              v-model="item.number"
              :val="item.id"
              :callback="cartNumChange"
            ></yd-spinner>
          </div>
        </yd-checklist>
      </div>
      <div v-else class="no-coupon">
        <img src="@/assets/kll/normal-cart.jpg" alt="" />
        <!-- <div class="text">购物车暂无商品</div> -->
      </div>
    </div>
    <div v-if="checkItemList.length" class="bottom-bar">
      <yd-checkbox
        color="#0078FF"
        v-model="isCheckAll"
        shape="circle"
        :change="checkAll"
        >全选
      </yd-checkbox>
      <div>
        <button @click="cartDelete" :style="{background: subColor}">
          删除
        </button>
        <button class="check" @click="buy" :style="{background: mainColor}">
          结算
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { getCartList, cartDelete, cartNumChange } from "@/api/mall";
import { mapState } from "vuex";
export default {
  data() {
    return {
      tab: 0,
      cartList: [],
      checkItemList: [],
      isCheckAll: false
    };
  },
  computed: {
    ...mapState({
      level: state => state.user.level,
      mainColor: state => state.app.mainColor,
      subColor: state => state.app.subColor
    })
  },
  watch: {
    tab: function(val) {
      this.cartList = [];
      this.checkItemList = [];
      this.isCheckAll = false;
      this.getCartList(val);
    }
  },
  methods: {
    getCartList(type) {
      getCartList({ status: type }).then(res => {
        if (res.status === 200) {
          this.cartList = res.data.data;
        }
      });
    },
    change(value, isCheckAll) {
      this.isCheckAll = isCheckAll;
    },
    checkAll() {
      this.isCheckAll = !this.isCheckAll;
      this.$refs.checklist.$emit("ydui.checklist.checkall", this.isCheckAll);
    },
    cartDelete() {
      cartDelete({
        id: this.checkItemList
      }).then(res => {
        if (res.status === 200) {
          this.$dialog.toast({
            mes: "删除成功",
            timeout: 1000,
            icon: "success"
          });
          let listHash = {};
          for (let i = 0; i < this.cartList.length; i++) {
            let flag = true;
            for (let j = 0; j < this.checkItemList.length; j++) {
              if (this.checkItemList[j] === this.cartList[i].id) {
                flag = false;
              }
            }
            if (flag) {
              listHash[this.cartList[i].id] = this.cartList[i];
            }
          }
          let arr = [];
          for (let key in listHash) {
            arr.push(listHash[key]);
          }
          this.cartList = arr;
          this.isCheckAll = false;
          this.$refs.checklist.$emit("ydui.checklist.checkall", false);
        }
      });
    },
    cartNumChange(val, num) {
      cartNumChange({ id: val, number: num });
    },
    buy() {
      if (this.checkItemList.length) {
        this.$router.push({
          path: "/payment",
          query: {
            type: "cart",
            cart: this.checkItemList.join(",")
          }
        });
      }
    }
  },
  created() {
    this.getCartList(0);
  },
  components: {}
};
</script>

<style lang="less" scoped>
.container {
  min-height: 100%;
  height: auto;

  .top-nav {
    background: #fff;
    height: 1.76rem;
    display: flex;
    justify-content: center;
    color: rgba(68, 68, 68, 1);
    font-size: 0.58rem;

    .nav-cell {
      width: 3.5rem;
      text-align: center;
      margin: 0.5rem 1.34rem;
      transition-duration: 0.1s;

      span {
        transition-duration: 0.2s;
        display: inline-block;
        height: 0.8rem;
      }
    }

    .active {
      font-weight: bold;
      font-size: 0.64rem;

      span {
        border-bottom: 0.24rem solid rgba(0, 120, 255, 1);
      }
    }
  }
  .cart-list {
    // border-radius: 0.24rem;
    overflow: hidden;
    margin: 0 3.2%;
    .cart-cell {
      display: flex;
      margin-top: 0.48rem;
      font-size: 0.56rem;
      background: #fff;
      position: relative;
      padding: 0.48rem;
      padding-left: 1.5rem;
      .left {
        width: 3.2rem;
        height: 3.2rem;
        background-color: #dfdfdf;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .cart-number {
        position: absolute;
        right: 3.2%;
        bottom: 0.24rem;
      }

      .middle {
        padding-left: 0.48rem;
        width: calc(100% - 3.2rem);

        .item-name {
          color: #444444;
          height: 1.5rem;
          overflow: hidden;
        }

        .item-size {
          color: #999999;
          margin: 0.1rem 0;
          font-size: 0.48rem;
        }
        .price {
          color: #f85859;
          font-size: 0.64rem;
          font-weight: bold;
        }
      }
    }
  }
  .no-coupon {
    text-align: center;
    // padding-top: 3rem;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;

    img {
      // height: 8rem;
      width: 100%;
    }

    .text {
      color: rgba(68, 68, 68, 1);
      font-size: 0.58rem;
    }
  }
  .bottom-bar {
    background: #fff;
    position: fixed;
    height: 2rem;
    display: flex;
    justify-content: space-between;
    line-height: 2rem;
    padding: 0 3.2%;
    width: 100%;
    bottom: 2rem;
    left: 0;
    z-index: 99;
    button {
      color: #444444;
      font-size: 0.64rem;
      height: 1.44rem;
      border: none;
      background: rgba(233, 233, 233, 1);
      border-radius: 0.08rem;
      width: 3rem;
      margin-top: 0.28rem;
      margin-left: 0.24rem;
    }
    .check {
      background-color: #0078ff;
      color: #fff;
    }
  }
}
</style>
